﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SN.Models;
using System.Collections;
using System.Text.RegularExpressions;
namespace SN.Report
{
    public partial class BatchInputFormulaDlg : Form
    {
        public FormTemp FrmTemp;

        private r_Zbzd zbzd;

        public r_Zbzd Zbzd
        {
            get { return zbzd; }
            set { zbzd = value; }
        }

        public BatchInputFormulaDlg()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string lsFormula = this.richTextBox1.Text.Trim().ToLower();
            //判断字符长度不能超过1000
            if (lsFormula.Length > 1000)
            {
                MessageBox.Show("公式太长啦!", "提示!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            //首先判断该表达式是否有效
            if (this.richTextBox1.Text.IndexOf(zbzd.Zbzd_Zbbh) >= 0)
            {
                MessageBox.Show("指标公式不合法，存在循环引用!");
                return;
            }
            int iSheet = FrmTemp.cellControl1.axCell.GetCurSheet();
            //写开始行 与开始列 （即指标公式所在的行与列)
            FrmTemp.cellControl1.axCell.SetCellTip(FrmTemp.startCol, FrmTemp.startRow, iSheet, this.richTextBox1.Text);
            zbzd.Zdzd_Gslx = "Calc";
            zbzd.Zbzd_Formula = this.richTextBox1.Text.Trim();
            Regex reg = new Regex(@"[\u4e00-\u9fa5]");//正则表达式( 包含汉字)
            if ((reg.IsMatch(lsFormula)) || lsFormula.Contains("select"))
                zbzd.Zdzd_Gslx = "Sql";
            zbzd.Attach();
            SN.Data.DBUtility.DB.Save<r_Zbzd>(zbzd);

            Close();
        }

        private void BatchInputFormulaDlg_Load(object sender, EventArgs e)
        {
            //常用函数
            lbfunList.Items.Add("Fe含量(槽号,天数)");
            lbfunList.Items.Add("Si含量(槽号,天数)");
            lbfunList.Items.Add("原铝品位(槽号,天数)");
            //
            lbfunList.Items.Add("出铝量(工区,天数)");
            lbfunList.Items.Add("日平均电压(工区,天数)");
            lbfunList.Items.Add("月平均电压(工区,月份)");
            //氧化铝浓度
            lbfunList.Items.Add("超35以上氧化铝浓度(工区,天数)");
            lbfunList.Items.Add("日平均氧化铝浓度(工区,天数)");
            this.Text = zbzd.Zbzd_Zbbh + this.Text;
            //指标公式类型
            if (zbzd.Zdzd_Gslx == null) zbzd.Zdzd_Gslx = "sql";

            if (zbzd.Zdzd_Gslx.ToLower() == "sql")
                checkBox2.Checked = true;
            else
                checkBox1.Checked = true;

            this.richTextBox1.Text = zbzd.Zbzd_Formula; //指标计算表达式
        }

        private void button3_Click(object sender, EventArgs e)
        {
            SN.Services.Extfunction ext = new Services.Extfunction();
            if (this.richTextBox1.Text.Trim().Length == 0) return;
            string sTemp = this.richTextBox1.Text.Trim().ToLower();
            if (sTemp.Contains("fe含量"))
                sTemp = ext.GetFe含量_Sql("1001", "4", DateTime.Now);
            else if (sTemp.Contains("si含量"))
                sTemp = ext.GetSi含量_Sql("1001", "4", DateTime.Now);
            else if (sTemp.Contains("出铝量"))
                sTemp = ext.Get出铝量_Sql("11", "1", DateTime.Now);
            else if (sTemp.Contains("原铝品位"))
                sTemp = ext.Get原铝品位_Sql("1001", "4", DateTime.Now);
            else if (sTemp.Contains("日平均电压"))
                sTemp = ext.Get日平均电压_Sql("11", "4", DateTime.Now);
            else if (sTemp.Contains("月平均电压"))
                sTemp = ext.Get月平均电压_Sql("11", "11", DateTime.Now);
            else if (sTemp.Contains("超35以上氧化铝浓度"))
                sTemp = ext.Get超35以上氧化铝浓度_Sql("11", "4", DateTime.Now);
            else if (sTemp.Contains("日平均氧化铝浓度"))
                sTemp = ext.Get日平均氧化铝浓度_Sql("11", "4", DateTime.Now);
            if (sTemp.ToLower().Contains("select"))
            {
                sTemp = sTemp.Replace(":curyear", DateTime.Now.Year.ToString());
                sTemp = sTemp.Replace(":curmonth", DateTime.Now.Month.ToString());
                sTemp = sTemp.Replace(":curday", DateTime.Now.Day.ToString());
                sTemp = sTemp.Replace(":curdate", DateTime.Now.Date.ToString("yyyy-MM-dd"));
                try
                {
                    var d = SN.Data.DBUtility.DB.FromSql(sTemp).ToScalar();
                    sTemp = d.ToString();
                }
                catch (Exception)
                {
                    throw;
                }
            }


            ArrayList aExpriss = new ArrayList();
            //计算表达式
            object o = SN.Services.Expression.ComputeExpression(sTemp);

            MessageBox.Show(o.ToString());

        }

        private void button2_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.richTextBox1.Text += "+";
        }

        private void button5_Click(object sender, EventArgs e)
        {
            this.richTextBox1.Text += "-";
        }

        private void button7_Click(object sender, EventArgs e)
        {
            this.richTextBox1.Text += "*";
        }

        private void button6_Click(object sender, EventArgs e)
        {
            this.richTextBox1.Text += "/";
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked) checkBox2.Checked = false;
            this.btnYear.Enabled = false;
            this.btnMonth.Enabled = false;
            this.btnDay.Enabled = false;
            this.btnDate.Enabled = false;
            this.lbfunList.Enabled = false;
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox2.Checked) checkBox1.Checked = false;
            this.btnYear.Enabled = true;
            this.btnMonth.Enabled = true;
            this.btnDay.Enabled = true;
            this.btnDate.Enabled = true;
            this.lbfunList.Enabled = true;
        }

        private void btnYear_Click(object sender, EventArgs e)
        {
            this.richTextBox1.Text += ":CurYear";
        }

        private void btnMonth_Click(object sender, EventArgs e)
        {
            this.richTextBox1.Text += ":CurMonth";
        }

        private void btnDay_Click(object sender, EventArgs e)
        {
            this.richTextBox1.Text += ":CurDay";
        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {
            label3.Text = string.Format("已经输入:{0}", this.richTextBox1.TextLength.ToString());
        }

        private void button8_Click(object sender, EventArgs e)
        {
            this.richTextBox1.Text += ":CurDate";
        }

        private void lbfunList_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.richTextBox1.Text += lbfunList.SelectedItem.ToString();
        }
    }
}
